﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Icst.Leaflike.Data;
using System.IO;


namespace Icst.Leaflike.Web.gather
{
    public partial class add : pagegather
    {
        protected int? GatherId { get { return !string.IsNullOrEmpty(Request["gid"]) ? (int?)int.Parse(Request["gid"]) : null; } }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (GatherId != null)
                {
                    tree_gather g = Gathering.Instance.FindGather(GatherId.Value);
                    txtLink.Text = g.image_link;
                    txtSite.Text = g.site;
                    txtTitle.Text = g.title;
                    txtDescription.Text = g.description;
                }
                btnDelete.Visible = btnDelete1.Visible = GatherId != null;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (uid != -1)
            {
                tree_gather g = new tree_gather()
                {
                    image_link = txtLink.Text,
                    site = txtSite.Text,
                    title = txtTitle.Text,
                    description = txtDescription.Text,
                    uid = uid
                };
                if (GatherId != null)
                {
                    g.gather_id = GatherId.Value;
                    Gathering.Instance.UpdateGather(g);
                }
                else
                {
                    g.created = DateTime.Now;
                    Gathering.Instance.InsertGather(g);
                }
            }
            PageExtension.Redirect(this, "gather_default.htm?sort=1" + "&uid=" + this.uid);
        }
        protected void uploadButton_Click(object sender, EventArgs e)
        {
            if (Uploader.HasFile && Uploader.PostedFile != null)
            {
                string fileName = uid + "_" + Uploader.FileName;
                string uploadPath = Path.Combine(Server.MapPath("~/gather"), fileName);
                Uploader.PostedFile.SaveAs(uploadPath);
                txtLink.Text = "gather/" + fileName;
            }
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Gathering.Instance.DeleteGather(GatherId.Value);
            PageExtension.Redirect(this,"gather_default.htm?sort=1" + "&uid=" + this.uid);
        }
    }
}
